package com.nf.mapper;


import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.nf.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

@Repository
public class TestDaoImpl implements TestDao {
    @Autowired
    private ComboPooledDataSource pool;

    @Override
    public Student getStudent(int account) {
        try{
            Connection connection = pool.getConnection();
            PreparedStatement pst = connection.prepareStatement("select * from student where id=?");
            pst.setInt(1,account);
            ResultSet rs = pst.executeQuery();
            Student student = null;
            if(rs.next()){
                student = new Student();
                student.setId(rs.getInt("id"));
                student.setName( rs.getString("name") );
                student.setAge( rs.getInt("age")  );
                return student;
            }else {
                rs.close();
                pst.close();
                connection.close();
                return null;
            }
        }catch (Exception e){
            return null;
        }
    }

    @Override
    public boolean delStudent() {
        return false;
    }

    @Override
    public boolean addStudent() {
        return false;
    }
}
